home *** CD-ROM | disk | FTP | other *** search
/ Shareware Super Platinum 8 / Shareware Super Platinum 8.iso / mac / WIN_PRO / WTEK0593.ZIP;1 / SWITCH.ZIP / TEXTWIN.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-08  |  7.8 KB  |  230 lines

  1. /*------------------------------------------------------------------------*/
  2. /*                                                                        */
  3. /*  TEXTWIN.CPP                                                           */
  4. /*                                                                        */
  5. /*  Implements non-inline functions from TEXTWIN.H                        */
  6. /*                                                                        */
  7. /*------------------------------------------------------------------------*/
  8.  
  9. #include "textwin.h"
  10.  
  11. /*------------------------------------------------------------------------*/
  12. /*                                                                        */
  13. /*  Member functions and static member data of Dispatcher                 */
  14. /*                                                                        */
  15. /*------------------------------------------------------------------------*/
  16.  
  17. Dispatcher *Dispatcher::Window = 0;
  18.  
  19. LONG FAR PASCAL _export 
  20. Dispatcher::Callback( HWND hnd, UINT msg, UINT w, LONG l )
  21. {
  22.     LONG res;
  23.     if( !Window->Dispatch( msg, w, l, res ) )
  24.         return DefWindowProc( hnd, msg, w, l );
  25.     else
  26.         return res;
  27. }
  28.  
  29. /*------------------------------------------------------------------------*/
  30. /*                                                                        */
  31. /*  Member functions and static member data of WinData                    */
  32. /*                                                                        */
  33. /*------------------------------------------------------------------------*/
  34.  
  35. BI_SVectorImp<DispatchRecord<WinData> > WinData::Map( 10, 5 );
  36.  
  37. int WinData::Dispatch( UINT msg, UINT w, LONG l, LONG& res )
  38. {
  39.     unsigned loc = Map.find( DispatchRecord<WinData>( msg, VoidType, 0 ) );
  40.     if( loc == UINT_MAX )
  41.         return 0;
  42.     else
  43.         {
  44.         DispatchRecord<WinData> entry = Map[loc];
  45.         DispatchRecord<WinData>::VoidFunc proc = entry.Proc;
  46.         switch( entry.Type )
  47.             {
  48.             case VoidType:
  49.                 res = (this->*(DispatchRecord<WinData>::VoidFunc)proc)();
  50.                 break;
  51.             case UUType:
  52.                 res = (this->*(DispatchRecord<WinData>::UUFunc)proc)( LOWORD(l),
  53.                                                               HIWORD(l) );
  54.                 break;
  55.             case UType:
  56.                 res = (this->*(DispatchRecord<WinData>::UFunc)proc)( w );
  57.                 break;
  58.             }
  59.         return 1;
  60.         }
  61. }
  62.  
  63. long WinData::OnSize( unsigned w, unsigned h )
  64. {
  65.     WinHeight = h;
  66.     WinWidth = w;
  67.     Resized();
  68.     return 0;
  69. }
  70.  
  71. void WinData::SetHandle( HWND handle)
  72. {
  73.     Handle = handle;
  74.     if( Handle == 0 )
  75.         return;
  76.     HDC hdc = GetDC( Handle );
  77.     TEXTMETRIC tm;
  78.     GetTextMetrics( hdc, &tm );
  79.     CharHeight = tm.tmHeight + tm.tmExternalLeading;
  80.     CharWidth = tm.tmAveCharWidth;
  81.     ReleaseDC( Handle, hdc );
  82. }
  83.  
  84. /*------------------------------------------------------------------------*/
  85. /*                                                                        */
  86. /*  Member functions and static member data of TextWindow                 */
  87. /*                                                                        */
  88. /*------------------------------------------------------------------------*/
  89.  
  90. int TextWindow::MaxWidth = FindWidest();
  91.  
  92. BI_SVectorImp<DispatchRecord<TextWindow> > TextWindow::Map( 10, 5 );
  93.  
  94. int TextWindow::Dispatch( UINT msg, UINT w, LONG l, LONG& res )
  95. {
  96.     unsigned loc = Map.find( DispatchRecord<TextWindow>( msg, VoidType, 0 ) );
  97.     if( loc == UINT_MAX )
  98.         {
  99.         if( WinData::Dispatch( msg, w, l, res ) )
  100.             return 1;
  101.         else
  102.             return 0;
  103.         }
  104.     else
  105.         {
  106.         DispatchRecord<TextWindow> entry = Map[loc];
  107.         DispatchRecord<TextWindow>::VoidFunc proc = entry.Proc;
  108.         switch( entry.Type )
  109.             {
  110.             case VoidType:
  111.                 res = (this->*(DispatchRecord<TextWindow>::VoidFunc)proc)();
  112.                 break;
  113.             case UUType:
  114.                 res = (this->*(DispatchRecord<TextWindow>::UUFunc)proc)( LOWORD(l),
  115.                                                               HIWORD(l) );
  116.                 break;
  117.             case UType:
  118.                 res = (this->*(DispatchRecord<TextWindow>::UFunc)proc)( w );
  119.                 break;
  120.             }
  121.         return 1;
  122.         }
  123. }
  124.  
  125. int TextWindow::RegisterClass( HWND instance )
  126. {
  127.     WNDCLASS wndClass;
  128.     wndClass.style = CS_HREDRAW | CS_VREDRAW;
  129.     wndClass.lpfnWndProc = &Dispatcher::Callback;
  130.     wndClass.cbClsExtra = 0;
  131.     wndClass.cbWndExtra = 0;
  132.     wndClass.hInstance = instance;
  133.     wndClass.hIcon = LoadIcon( 0, IDI_APPLICATION );
  134.     wndClass.hCursor = LoadCursor( 0, IDC_ARROW );
  135.     wndClass.hbrBackground = GetStockObject( WHITE_BRUSH );
  136.     wndClass.lpszMenuName = 0;
  137.     wndClass.lpszClassName = "TextWindow";
  138.     return ::RegisterClass( &wndClass );
  139. }
  140.  
  141. int TextWindow::CreateWindow( HWND instance, DWORD Flags )
  142. {
  143.     SetHandle( ::CreateWindow( "TextWindow",
  144.                                "Text Sample",
  145.                                WS_OVERLAPPEDWINDOW | Flags,
  146.                                CW_USEDEFAULT, CW_USEDEFAULT,
  147.                                CW_USEDEFAULT, CW_USEDEFAULT,
  148.                                0, 0,
  149.                                instance,
  150.                                0 ) );
  151.     return GetHandle() != 0;
  152. }
  153.  
  154. void TextWindow::Create( HWND instance, int show, DWORD flags )
  155. {
  156.     if( !RegisterClass( instance ) || !CreateWindow( instance, flags ) )
  157.         return;
  158.     ShowWindow( GetHandle(), show );
  159.     UpdateWindow( GetHandle() );
  160. }
  161.  
  162. char *TextWindow::Text[] =
  163. {
  164.     "Give thy thoughts no tongue,",
  165.     "Nor any unproportion'd thought his act.",
  166.     "Be thou familiar, but by no means vulgar;",
  167.     "The friends thou hast, and their adoption tried,",
  168.     "Grapple them to thy soul with hoops of steel;",
  169.     "But do not dull thy palm with entertainment",
  170.     "Of each new-hatch'd unfledg'd comrade.  Beware",
  171.     "Of entrance to a quarrel, but being in,",
  172.     "Bear't that the opposed may beware of thee.",
  173.     "Give every man thine ear, but few thy voice;",
  174.     "Take each man's censure, but reserve thy judgement.",
  175.     "Costly thy habit as thy purse can buy,",
  176.     "But not express'd in fancy; rich, not gaudy;",
  177.     "For the apparel oft proclaims the man,",
  178.     "And they in France of the best rank and station",
  179.     "Are most select and generous, chief in that.",
  180.     "Neither a borrower nor a lender be;",
  181.     "For loan oft loses both itself and friend,",
  182.     "And borrowing dulls the edge of husbandry.",
  183.     "This above all: to thine own self be true,",
  184.     "And it must follow, as the night the day,",
  185.     "Thou canst not then be false to any man.",
  186.     "        W. Shakespeare,",
  187.     "        \"Hamlet, Prince of Denmark\",",
  188.     "        Act 1, Scene 4."
  189. };
  190.  
  191. int TextWindow::Lines()
  192. {
  193.     // WARNING: do not move this function. It must follow the
  194.     // definition of TextWindow::Text, so that sizeof() can be
  195.     // used to determine how much text is actually present.
  196.     return sizeof(Text)/sizeof(*Text);
  197. }
  198.  
  199. int TextWindow::FindWidest()
  200. {
  201.     int widest = 0;
  202.     for( int i = 0; i < Lines(); i++ )
  203.         {
  204.         int width = strlen( Text[i] );
  205.         if( width > widest )
  206.             widest = width;
  207.         }
  208.     return widest;
  209. }
  210.  
  211. long TextWindow::OnPaint()
  212. {
  213.     PAINTSTRUCT ps;
  214.     HDC hdc = BeginPaint( GetHandle(), &ps );
  215.     int XPos = GetCharWidth()*(1 - GetXPos() );
  216.     for( int i = 0; i < Lines(); i++ )
  217.         TextOut( hdc,
  218.                  XPos, GetCharHeight()*(i+1-GetYPos()),
  219.                  Text[i], strlen(Text[i]) );
  220.     EndPaint( GetHandle(), &ps );
  221.     return 0;
  222. }
  223.  
  224. long TextWindow::OnDestroy()
  225. {
  226.     PostQuitMessage(0);
  227.     return 0;
  228. }
  229.  
  230.